package com.zpark.drug.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.mapper.SalesRecordsMapper;
import com.zpark.drug.pojo.SalesRecords;
import com.zpark.drug.service.SalesRecordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class SalesRecordsServiceImpl implements SalesRecordsService {
    @Autowired
    private SalesRecordsMapper salesRecordsMapper;

    @Override
    public List<SalesRecords> findAll() {
        return salesRecordsMapper.selectAll();
    }

    @Override
    public SalesRecords findById(Integer id) {
        return salesRecordsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addSalesRecords(SalesRecords salesRecords) {
        return salesRecordsMapper.insert(salesRecords);
    }

    @Override
    public int updateSalesRecords(SalesRecords salesRecords) {
        return salesRecordsMapper.updateByPrimaryKey(salesRecords);
    }

    @Override
    public int deleteSalesRecordsById(Integer id) {
        return salesRecordsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SalesRecords> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(SalesRecords.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param)) {
            criteria.orLike("name", "%" + param + "%")
                    .orLike("person", "%" + param + "%");
        }
        return new PageInfo<>(salesRecordsMapper.selectByExample(example));
    }

}
